/**
 * http://jaxb.java.net/2.2.3/
 * 实用数据绑定: 考察 JAXB
 *   http://www.ibm.com/developerworks/cn/xml/x-pracdb2.html
 */
package org.demo.xml;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.demo.xml.bean.DetailInfoBean;
import org.demo.xml.bean.GraphBean;
import org.demo.xml.bean.LineBean;
import org.demo.xml.bean.TrendLinesBean;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * xml 测试
 * @date 2009-12-8 下午07:22:07
 */
public class XMLUtil {
	
    /**
     * 使用 dom4j 生成 xml
     */
    public String generate() {
        
        ByteArrayOutputStream bos = null;
        try {
            bos = new ByteArrayOutputStream(1024);
            // 新建 format 用来格式化 xml 文件
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");                         // 编码格式
            format.setIndentSize(4);                             // Tab 缩进
            XMLWriter writer = new XMLWriter(bos, format);
            Document document = DocumentHelper.createDocument(); // 创建文档
            Element graph = document.addElement("graph");        // 创建根节点
            graph.addAttribute("caption", "Monthly Unit Sales");
            // add set 
            Element node = graph.addElement("set");
            node.addAttribute("name", "Jan");
            node.addAttribute("value", "462");
            // add trendlines 
            node = graph.addElement("trendlines");
            // add line 
            node = node.addElement("line");
            node.addAttribute("startValue", "700");
            node.addAttribute("color", "91c728");
            
            writer.write(document);
            writer.flush();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe) {
        }
        return bos.toString();
    }
    
    /**
     * 使用 jaxb2.0 生成 xml
     */
    public void jaxb(){
    	GraphBean graph = new GraphBean();
    	graph.setCaption("Monthly Unit Sales");
    	graph.addDetail(new DetailInfoBean("Jan", "462"));
    	graph.setTrendLines(getTrendLines());

    	// 
    	JAXBContext context;
		try {
			context = JAXBContext.newInstance(GraphBean.class);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			
			marshaller.marshal(graph, System.out);
			System.out.flush();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
    }
    
    private TrendLinesBean getTrendLines(){
    	TrendLinesBean bean = new TrendLinesBean();
    	bean.addLine(new LineBean("700", "91c728"));
    	return bean;
    }
    
    /**
     * 使用 jaxb2.0 从 xml 反序列化到 javabean
     */
    public void jaxbFromXml(){
    	try {
    		File file = new File("d:/Temp/test.xml");
    		
    		JAXBContext context = JAXBContext.newInstance(GraphBean.class);
        	Unmarshaller unmarshaller = context.createUnmarshaller();
			GraphBean root = (GraphBean)unmarshaller.unmarshal(file);

			// 
			System.out.println(root.getDetailList().get(0).getName());
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
    	XMLUtil t = new XMLUtil();
    	// System.out.println(t.generate());
    	t.jaxb();
    	// t.jaxbFromXml();
	}
}